﻿@page "/login"
@using nERP.ViewModel.HomeVMs
@inherits BasePage

<div class="container">
    <div class="row">
        <div class="col-12">
            <Card class="shadow-lg border-0 my-5">
                <BodyTemplate>
                    <div class="login-item login-item-avatar">
                        <div class="text-center">
                            @* <Avatar Url="/images/logo.png" Size="Size.ExtraExtraLarge" /> *@
                            <img class="img-logo" src="/_content/nERP.Shared/images/logo.png" alt="">
                            <h3 class="text-dark">ERP</h3>
                        </div>
                        <ValidateForm Model="@Model" OnValidSubmit="@Submit">
                            <BootstrapInput @bind-Value="@Model.ITCode" ShowLabel="false" PlaceHolder="@WtmBlazor.Localizer["Login.InputUserName"]" />
                            <BootstrapInput @bind-Value="@Model.Password" type="password" ShowLabel="false" PlaceHolder="@WtmBlazor.Localizer["Login.InputPassword"]" />
                            @if (WtmBlazor?.ConfigInfo?.EnableTenant == true)
                            {
                                <div class="row g-3 form-inline">
                                    <div class="col-12 col-sm-6">
                                        <BootstrapInput @bind-Value="@Model.Tenant" ShowLabel="false" PlaceHolder="@WtmBlazor.Localizer["Login.InputTenant"]" />
                                    </div>
                                    <div class="col-12 col-sm-6">
                                        <LinkButton IsVertical="true" Icon="fa-solid fa-pen" Text="无租户，创建一个" OnClick="OnCreateClick" />
                                    </div>
                                </div>
                            }
                            <Button ButtonType="ButtonType.Submit" Text="@WtmBlazor.Localizer["Login.Login"]" style="width:100%;margin-top:20px;" IsAsync="true" />
                        </ValidateForm>
                    </div>
                </BodyTemplate>
            </Card>
        </div>
    </div>
</div>



@code {
    [Parameter]
    public bool? LoginSuccess { get; set; }
    [Parameter]
    public EventCallback<bool?> LoginSuccessChanged { get; set; }
    [Parameter]
    public EventCallback OnUserInfoSet { get; set; }

    private int bgnumber;
    private LoginVM Model = new LoginVM();

    protected override void OnInitialized()
    {
        bgnumber = new Random().Next(1, 6);
        if (WtmBlazor.ConfigInfo.IsQuickDebug == true)
        {
            Model.ITCode = "admin";
            Model.Password = "000000";
        }
        base.OnInitialized();
    }

    private async Task Submit(EditContext context)
    {
        var rv = await WtmBlazor.Api.CallAPI<DynamicData>("/api/_account/loginjwt", HttpMethodEnum.POST, new { Account = Model.ITCode, Password = Model.Password, Tenant = Model.Tenant });
        if (rv.StatusCode == System.Net.HttpStatusCode.OK)
        {
            var token = rv.Data.Fields["access_token"].ToString();
            var rtoken = rv.Data.Fields["refresh_token"].ToString();
            await SetToken(token, rtoken);
            var userinfo = await WtmBlazor.Api.CallAPI<LoginUserInfo>("/api/_account/CheckUserInfo", HttpMethodEnum.GET, new { });

            if (userinfo.StatusCode == System.Net.HttpStatusCode.OK)
            {
                await SetUserInfo(userinfo.Data);
                await OnUserInfoSet.InvokeAsync();
                await LoginSuccessChanged.InvokeAsync(true);
            }
            else
            {
                await WtmBlazor.Toast.Error(WtmBlazor.Localizer["Sys.Error"], rv.Errors.Message[0]);
            }

        }
        else
        {
            await WtmBlazor.Toast.Error(WtmBlazor.Localizer["Sys.Error"], rv.Errors?.Message[0]);
        }
    }


    private async Task OnCreateClick()
    {
        if (await OpenDialog<nERP.Shared.Pages._Admin.FrameworkTenant.Create2>(WtmBlazor.Localizer["Sys.Create"], null, Size.ExtraSmall) == DialogResult.Yes)
        {

        }
    }
}
